package com.example.datamanage.controller;

import com.example.datamanage.common.Result;
import com.example.datamanage.service.SqlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * @author 28142
 * @description SqlController
 * @date 2023/7/25 14:05
 */
@RestController
@RequestMapping("/sql")
@Validated
public class SqlController {
    @Autowired
    private SqlService sqlService;

    /**
     * 执行前端传入的sql语句
     * @author 杨雨盛
     * @date 2023/7/25 17:41 
     * @param sql 
     * @return com.example.datamanage.common.Result
     */
    // @GetMapping
    // public Result executeSql(@RequestParam String sql) {
    //     return sqlService.executeSql(sql);
    // }

    /**
     * 导出sql查询结果
     * @author 杨雨盛
     * @date 2023/7/26 14:54
     * @param sql
     * @param response
     */
    @GetMapping("/export/{type}/{sql}")
    public void export(@NotBlank(message = "请选择文件类型")
                       @PathVariable String type,
                       @PathVariable String sql,
                       HttpServletResponse response) {
        System.out.println(type);
        System.out.println(sql);
        sqlService.export(type, sql, response);
    }

    /**
     * 执行前端传入的sql查询
     * @author 杨雨盛
     * @date 2023/7/29 11:42
     * @param mulSql
     * @return com.example.datamanage.common.Result
     */
    @GetMapping
    public Result executeQuery(@RequestParam String mulSql) {
        return sqlService.executeQuery(mulSql);
    }

    /**
     * 查询当前库下所有表，不包括视图
     * @author 杨雨盛
     * @date 2023/7/30 21:10
     * @return com.example.datamanage.common.Result
     */
    @GetMapping("/tables")
    public Result showTables() {
        return sqlService.showTables();
    }

    /**
     * 查询当前库下所有视图
     * @author 杨雨盛
     * @date 2023/7/30 21:12
     * @return com.example.datamanage.common.Result
     */
    @GetMapping("/views")
    public Result showViews() {
        return sqlService.showViews();
    }
}
